package com.shop2cn.iapi.sqmarketing.testcase;

import java.math.BigDecimal;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqmarketing.parameter.DeleteByIdReq;
import com.shop2cn.iapi.sqmarketing.pojo.ActivityConditionItemPojo;
import com.shop2cn.iapi.sqmarketing.service.DeleteByIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqmarketingService;
import com.ymttest.database.model.sqmarketing.MarketingActivity;
import com.ymttest.database.sqlwapper.sqmarketing.MarketingActivityWrapper;

public class Ts_DeleteById {

	// java接口
	private static DeleteByIdReq deleteByIdReq;
	private static DeleteByIdCall deleteByIdCall;
	
	private static MarketingActivityWrapper marketingActivityWrapper = new MarketingActivityWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据活动id删除活动");
	}

	@Before
	public void caseUp() {
		deleteByIdReq = new DeleteByIdReq();
		deleteByIdCall = new DeleteByIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deleteByIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb() throws Exception {
		
		MarketingActivity actual = marketingActivityWrapper
				.selectByPrimaryKey(deleteByIdReq.getId());

		Logger.verifyEquals((byte) 0, actual.getDelete_flag(), "验证Delete_flag");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_DeleteById_001() {
		Logger.start(true, "删除活动");

		try {
			
			// 删除之前的营销活动，避免条件重复报错
			SqmarketingService
					.deleteAllMarketingActivity(1234L);

			// 清除营销活动缓存
			SqmarketingService.deleteCache();

			// 创建满金额赠优惠条件
			ActivityConditionItemPojo activityConditionItem = new ActivityConditionItemPojo();

			activityConditionItem.setConditionType(1);
			activityConditionItem.setItemList(Arrays.asList("ALL"));

			// 创建满金额赠优惠活动
			Long id = SqmarketingService.saveOrUpdaateMarketingActivity(2, "CNY",
					BigDecimal.ONE, 1234L, 1,
					Arrays.asList(activityConditionItem));

			deleteByIdReq.setId(id);
			deleteByIdReq.setSellerId(1234L);

			deleteByIdCall.setData(deleteByIdReq);
			deleteByIdCall.callService();

			Logger.verifyEquals(true, deleteByIdCall.getSuccess(),
					"验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
